package com.sunrise.xdoc.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.sunrise.xdoc.entity.finance.PhieuThanhToan;
import com.sunrise.xdoc.entity.finance.PhieuThanhToan.LoaiThanhToan;
import com.sunrise.xdoc.entity.order.DonHang;
import com.sunrise.xdoc.entity.order.DonHang.TrangThaiDonHang;
import com.sunrise.xdoc.service.AbstractService;
import com.sunrise.xdoc.service.IFinanceService;

@Service("financeService")
public class FinanceService extends AbstractService implements IFinanceService {
	private static final long serialVersionUID = -3630389821681789803L;

	@Override
	public void thanhToanDonHang(Long donHangId) {
		DonHang donHang = getDaoHelper().getDao(DonHang.class).findById(donHangId);
		// Kiểm tra trạng thái đơn hàng
		if (donHang.getTrangThai() == TrangThaiDonHang.DA_THANH_TOAN) {
			// TODO throw exception
		}

		// Chuyển trạng thái đơn hàng --> Đã thanh toán
		donHang.setTrangThai(TrangThaiDonHang.DA_THANH_TOAN);
		donHang.setThoiGianThanhToan(new Date());
		getDaoHelper().getDao(DonHang.class).saveOrUpdate(donHang);

		// Tạo phiếu thanh toán
		PhieuThanhToan phieuThanhToan = new PhieuThanhToan();
		phieuThanhToan.setDonHang(donHang);
		phieuThanhToan.setLoai(LoaiThanhToan.DON_HANG);
		phieuThanhToan.setMaNhanVien("admin"); // TODO change later
		phieuThanhToan.setSoTien(donHang.getTongGiaThanhToan());
		getDaoHelper().getDao(PhieuThanhToan.class).saveOrUpdate(phieuThanhToan);
	}

}
